package com.sx.zongzhi.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.VersionBean;
import com.sx.zongzhi.receiver.UpdateService;

/**
 *
 * @author	wenjie
 *	版本更新的工具类
 */
public class UpdateVersionUtil {

    /**
     * 弹出新版本提示
     * @param context 上下文
     * @param rl
     * @param bean b版本的bean
     */
    public static void showDialog(final Context context, View rl, final VersionBean bean){
        try {

            View view = LayoutInflater.from(context).inflate(R.layout.version_update_dialog, null);
            final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT, true);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x90000000));
            popupWindow.showAtLocation(rl, 0, 0, Gravity.CENTER);

            final Button btnOk = (Button) view.findViewById(R.id.btn_update_id_ok);
            if (bean.getFile()!=null){
                long size=bean.getFile().getFileSize();
                btnOk.setText("更新("+1.0f*((int)(1.0f*size/1024/1024*100))/100+"M)");
            }
            Button btnCancel = (Button) view.findViewById(R.id.btn_update_id_cancel);
            TextView tvContent = (TextView) view.findViewById(R.id.tv_Content);
            if (BaseUtil.isEmpty(bean.getDesc()))return;
            String str=(bean.getDesc().replaceAll("；","\n"));
            tvContent.setText(str);
            btnOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                    if(v.getId() == R.id.btn_update_id_ok){
                        if (bean.getFile()!=null){
                            Intent intent = new Intent(context,UpdateService.class);
                            intent.putExtra("APK_PATH",bean.getFile().getPath());
                            context.startService(intent);

                        }
                    }
                }
            });
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
